﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.admin.Administrator
{
    public partial class AdministratorMasterForm : BasicPage
    {
        public int Pcount = 0; //总条数
        public int PageIndex; //当前页
        public readonly int Pagesize = 15; //设置每页显示的大小
        private readonly AdministratorBLL _bll = new AdministratorBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            chkLoginLevel("viewAdministrator");
            RptBind();
        }

        private void RptBind()
        {
            if (!int.TryParse(Request.Params["page"] as string, out this.PageIndex))
            {
                this.PageIndex = 0;
            }
            var qcmodel = new Pagination { PageSize = Pagesize, PageIndex = PageIndex };
            this.Pcount = _bll.GetCount(qcmodel);
            this.lbtnDel.Enabled = this.Pcount > 0;
            this.rptList.DataSource = _bll.GetPageList(qcmodel);
            this.rptList.DataBind();
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delAdministrator");
            var list = new List<AdministratorInfo>();
            for (var i = 0; i < rptList.Items.Count; i++)
            {
                var id = ((Label)rptList.Items[i].FindControl("lb_id")).Text;
                var cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                if (!cb.Checked) continue;
                var model = new AdministratorInfo { Id = id };
                list.Add(model);
            }
            if (_bll.Delete(list))
            {
                JscriptPrint("批量删除成功！", "AdministratorMasterForm.aspx", "Success");
            }
            else
            {
                JscriptPrint("删除失败！", "AdministratorMasterForm.aspx", "Error");
            }
        }
    }
}